<template>
  <el-dialog
    :before-close="cancel"
    :close-on-click-modal="false"
    :visible.sync="addRoleDialogVisible"
    class="vc-dialog-form"
    title="新增角色"
    width="480px"
  >
    <el-form :model="addForm" :rules="rules" label-width="100px" ref="addForm" size="small">
      <input maxlength="10" readonly type="hidden" v-model="addForm.rgroupId" />
      <el-form-item label="角色名称" maxlength="20" prop="name">
        <el-input clearable maxlength="10" v-model="addForm.name"></el-input>
      </el-form-item>
      <el-form-item label="角色编码" maxlength="30" prop="code">
        <el-input clearable maxlength="10" v-model="addForm.code"></el-input>
      </el-form-item>

      <el-form-item label="描述" prop="descr" style="text-align:left">
        <el-input clearable maxlength="100" rows="2" type="textarea" v-model="addForm.descr"></el-input>
      </el-form-item>
    </el-form>

    <span class="dialog-footer" slot="footer">
      <el-button @click="cancel" size="small">取消</el-button>
      <el-button :disabled="disabled" @click="save" size="small" type="primary">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'addRole',

  props: ['addRoleDialogVisible'],
  data () {
    return {
      addForm: {
        code: '',
        rgroupId: '',
        name: '',
        descr: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          },
          {
            required: true,
            pattern: /^\S+(\s+\S+)*$/,
            message: '不能以空格开头或者结尾',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入编码',
            trigger: 'blur'
          },
          {
            required: true,
            pattern: /^\S+(\s+\S+)*$/,
            message: '不能以空格开头或者结尾',
            trigger: 'blur'
          },
          {
            validator: this.half,
            trigger: 'blur'
          }
        ]
      },
      disabled: false,
      readonly: true
    }
  },
  computed: {
    id () {
      return this.$parent.selectId // id指打开对话框之前父页面所选择的用户组id.
    }
  },
  watch: {
    addRoleDialogVisible (val) {
      if (!val) return
      this.$nextTick(() => {
        this.$refs.addForm && this.$refs.addForm.resetFields()
        this.addForm.rgroupId = this.id
        setTimeout(() => {
          this.$refs.addForm.clearValidate()
        }, 0)
      })
    }
  },
  methods: {
    /*
      编码
    */
    half (rule, value, callback) {
      const reg = /[^\x00-\xff]/gi
      if (!reg.test(value)) callback()
      callback(new Error('请输入半角字符'))
    },
    /**
     * 保存
     */
    save () {
      if (this.disabled) {
        return
      }
      this.$refs.addForm.validate(valid => {
        if (!valid) {
          return false
        }
        this.disabled = true
        this.$api
          .addRoleByRoleGroupId({
            data: this.addForm
          })
          .then(res => {
            this.disabled = false
            if (res.success) {
              this.$emit('close', res.data || {})
              this.$notifyBox('新增成功', 'success')
            }
          })
          .catch(() => {
            this.disabled = false;
          })
      })
    },
    /**
     * 取消
     */
    cancel () {
      this.$emit('close', false)
    }
  }
}
</script>
